package TUILayer;//makes this class a part of the TUILayer package
import ControlLayer.*;
import ModelLayer.*;
import java.util.*;

/**
 * 
 * User interface to the DVD Controller
 * <p>
 * <code>DvdUI</code> class manages information about DVDs and passes input data to the <code>ControlLayer</code>.
 * <br>
 * @author (dmaj0913 - group 2)
 * @version (2013.11.08)
 * <p>
 * <b><i>History:</i></b>
 * <p>
 * (KBH): 2005.03.31:
 * <p>
 * (FEN): 2006.02.24: Comments translated into English
 * <p>
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering and loose coupling
 * <p>                
 * (KBH): 2008.10.24: dvdmenu changed to If   
 * 
 */
public class DvdUI
{
    private DVDCtr dvdCtr;
    private ArrayList<DVD> dvdMemoryBackup;
    private boolean existence = false;
    /**
     * Constructor creates new empty collection of DVDs.
     */
    public DvdUI()
    {
        dvdMemoryBackup = new ArrayList<DVD>();
    }
    /**
     * Starts the User Interface of the DVD Controller(calls <code>dvdMenu</code> method) and returns a backup of collection of the DVDs.
     * @param backup keeps an backup of previous collection of dvds.
     * @return backup of the modified collection of the DVDs.
     */
    public ArrayList<DVD> start(ArrayList<DVD> backup)
    {
        dvdMemoryBackup = backup;
        dvdCtr = new DVDCtr(backup);
        if(dvdMemoryBackup.size() > 0) existence = true;
        dvdMenu();
        return dvdMemoryBackup;
    }
    
    /**
     * Manages the data flow in the terminal and passes information to the other layers.
     */
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if (choice == 1)
            {
                if(existence) findDVD();
            }
            else{if(choice == 2)
                {
                    existence = true;
                    createDVD();
                }
                else{
                    if(choice == 3)
                    {
                        if(existence) updateDVD();
                    }
                    else
                    if(choice == 4)
                    {
                        if(existence) createCopy();
                    }
                    else{
                        if(choice == 5)
                        {
                            if(existence) updateDVDCopy();
                        }
                        else
                        if(choice == 6)
                        {
                            if(existence) deleteDVD();
                        }
                        else
                        if(choice == 7)
                        {
                            if(existence) deleteDVDCopy();
                        }
                        else{
                            if(choice == 8)
                            {
                                if(existence) listAllDvds();
                            }
                            else
                            {
                                exit = true;
                                dvdMemoryBackup = dvdCtr.listAllDvds();
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    
    /**
    * Accepts input data and prints information in the Terminal.
    * @return number of type <code>Int</code> as a desired user choice.
    */
    private int writeDVDMenu()
    {
         Scanner keyboard = new Scanner(System.in);
         System.out.println("\f *** DVD Menu ***");
         System.out.println(" (1) Show DVD information");
         System.out.println(" (2) Create new DVD");
         System.out.println(" (3) Update DVD");
         System.out.println(" (4) Create DVD Copy");
         System.out.println(" (5) Update DVD Copy");
         System.out.println(" (6) Delete DVD");
         System.out.println(" (7) Delete DVD Copy");
         System.out.println(" (8) Show current DVD list");
         System.out.println(" (9) Return");
         if(!existence) System.out.println("NOTE: There are currently no DVDs created, please register one first");
         System.out.print("\n Make your choice: ");
         int choice = keyboard.nextInt();
         return choice;
    }
    
    /**
     * Finds an DVD by its ID, entered through the Terminal, and prints out all the information about it and its copies. 
     */
    private void findDVD()
    {     
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please write an id: ");
        long no = keyboard.nextLong();
        while(dvdCtr.findDVD(no) == null)
        {
            System.out.println("The DVD id you listed is not valid, please try again");
            no = keyboard.nextLong();
        }
        DVD dvd = dvdCtr.findDVD(no);
        System.out.println("DVD Information:");
        System.out.println("Id: " + dvd.getId());
        System.out.println("Title: " + dvd.getTitle());
        System.out.println("Artist: " + dvd.getArtist());
        System.out.println("Publication date: " + dvd.getDate());
        System.out.println("List of copies:");
        ArrayList<Copy> listOfCopies = dvd.getCopyList();
        Iterator<Copy> itcopy = listOfCopies.iterator();
        while(itcopy.hasNext())
        {
            Copy tocopy = itcopy.next();
            System.out.println("Serial Number: " + tocopy.getSerialNumber());
            System.out.println("Purchase Date: " + tocopy.getPurchaseDate());
            System.out.println("Purchase Cost: " + tocopy.getPurchaseCost());
            System.out.println();
        }
        System.out.println();
        System.out.println("Please, press ENTER to continue: ");
        keyboard.nextLine();
        keyboard.nextLine();
    }
    
    /**
     * Updates all fields(Serial Number, Purchase Date, Purchase Cost) of DVD Copy found by ID of the DVD and serial number of the Copy.
     */
    private void updateDVDCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please write the ID of the DVD whose copy you want updated: ");
        long id = keyboard.nextLong();
        while(dvdCtr.findDVD(id) == null)
        {
            System.out.println("The DVD id you input is invalid, please try again: ");
            id = keyboard.nextLong();
        }
        if(dvdCtr.findDVD(id).getCopyList().size() == 0)
        {
            System.out.println("There are no copies of this DVD, press Enter to continue...");
            keyboard.nextLine();
            keyboard.nextLine();
        }
        else
        {
            System.out.println("Please input the serial number of the copy you want updated: ");
            long no = keyboard.nextLong();
            while(dvdCtr.findCopy(dvdCtr.findDVD(id),no) == null)
            {
                System.out.println("The DVD does not have a copy with that serial number: ");
                no = keyboard.nextLong();
            }
            System.out.println("New serial number: ");
            long newno = keyboard.nextLong();
            while(dvdCtr.findCopy(dvdCtr.findDVD(id),newno) != null)
            {
                System.out.println("A copy with that serial number exists, please try again: ");
                newno = keyboard.nextLong();
            }
            keyboard.nextLine();
            System.out.println("New purchase date: ");
            String date = keyboard.nextLine();
            System.out.println("New purchase cost: ");
            int cost = keyboard.nextInt();
            dvdCtr.updateDVDCopy(dvdCtr.findDVD(id),no,newno,date,cost);
        }
    }
    
    /**
     * Creates a new DVD by entering in Terminal unique ID and desired title,artist and publication date.
     */
    private void createDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input the data for the new DVD on separate lines: ");
        long id; 
        System.out.println("ID: ");        
        id = keyboard.nextLong();
        String title, date, artist;
        while(dvdCtr.findDVD(id) != null)
        {
            System.out.println("The DVD id you input is already in the system, please try again: ");
            id = keyboard.nextLong();
        }
        title = keyboard.nextLine();
        System.out.println("Title: ");
        title = keyboard.nextLine();
        System.out.println("Artist: ");
        artist = keyboard.nextLine();
        System.out.println("Publication date: ");
        date = keyboard.nextLine();
        dvdCtr.createDVD(id,title,artist,date);
    }
    
    /**
     * Deletes a DVD Copy by entering the ID of the DVD and the Serial Number of the copy in the terminal.
     */
    private void deleteDVDCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please write the ID of the DVD whose copy you want deleted: ");
        long id = keyboard.nextLong();
        while(dvdCtr.findDVD(id) == null)
        {
            System.out.println("The DVD id you input is invalid, please try again: ");
            id = keyboard.nextLong();
        }
        if(dvdCtr.findDVD(id).getCopyList().size() == 0)
        {
            System.out.println("There are no copies of this DVD, press Enter to continue...");
            keyboard.nextLine();
            keyboard.nextLine();
        }
        else
        {
            System.out.println("Please input the serial number of the copy you want deleted: ");
            long no = keyboard.nextLong();
            while(dvdCtr.findCopy(dvdCtr.findDVD(id),no) == null)
            {
                System.out.println("The DVD does not have a copy with that serial number: ");
                no = keyboard.nextLong();
            }
            dvdCtr.findDVD(id).removeCopy(dvdCtr.findCopy(dvdCtr.findDVD(id),no));
        }
    }
    
    /**
     * Creates copy of a DVD by entering ID of the DVD and information about the copy(Serial Number, Purchase Date, Purchase Cost).
     */
    private void createCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input the data about the copy on separate lines: ");
        System.out.println("DVD id: ");
        long id = keyboard.nextLong();
        while(dvdCtr.findDVD(id) == null)
        {
            System.out.println("The DVD id you entered is not valid, please try again: ");
            id = keyboard.nextLong();
        }
        long number;
        int cost;
        String date;
        System.out.println("Serial number: ");
        number = keyboard.nextInt();
        while(dvdCtr.findCopy(dvdCtr.findDVD(id), number) != null)
        {
            System.out.println("The copy serial number you entered is invalid, please try again: ");
            number = keyboard.nextInt();
        }
        date = keyboard.nextLine();
        System.out.println("Purchase date: ");
        date = keyboard.nextLine();
        System.out.println("Purchase cost: ");
        cost = keyboard.nextInt();
        dvdCtr.createCopy(id,number,date,cost);
    }
    
    /**
     * Accepts an ID from the Terminal and deletes the DVD with this ID.
     */
    private void deleteDVD()
    {
        System.out.println("Please input the id of the DVD you want to delete: ");
        Scanner keyboard = new Scanner(System.in);
        long id = keyboard.nextLong();
        while(dvdCtr.findDVD(id) == null)
        {
            System.out.println("The DVD id you entered is not valid, please try again: ");
            id = keyboard.nextInt();
        }
        dvdCtr.deleteDVD(id);
        if(dvdCtr.listAllDvds().size() == 0) existence = false;
    }
    
    /**
     * Updates all the specifications of an DVD(ID, title, artist, publication date) by entering desired ID.
     */
    private void updateDVD()
    {
        System.out.println("Please input the id of the DVD you want to update: ");
        Scanner keyboard = new Scanner(System.in);
        long id = keyboard.nextLong();
        while(dvdCtr.findDVD(id) == null)
        {
            System.out.println("The DVD id you input is not valid, please try again: ");
            id = keyboard.nextLong();
        }
        System.out.println("New ID: ");
        long newid = keyboard.nextLong();
        while(dvdCtr.findDVD(newid) != null)
        {
            System.out.println("The DVD id you specified already exists in the database, please try again:");
            newid = keyboard.nextLong();
        }
        keyboard.nextLine();
        System.out.println("New title: ");
        String title = keyboard.nextLine();
        System.out.println("New artist: ");
        String artist = keyboard.nextLine();
        System.out.println("New publication date:");
        String date = keyboard.nextLine();
        dvdCtr.updateDVD(id,newid,title,artist,date);
    }
    
    /**
     * Prints out in the Terminal all the information for each DVD in the collection followed by its copies.
     */
    private void listAllDvds()
    {
        ArrayList<DVD> dvdList = dvdCtr.listAllDvds();
        System.out.println("List of all DVDs and their copies: ");
        Iterator<DVD> itdvd = dvdList.iterator();
        while(itdvd.hasNext())
        {
            DVD tolist = itdvd.next();
            System.out.println("DVD ID: " + tolist.getId());
            System.out.println("Title: " + tolist.getTitle());
            System.out.println("Artist: " + tolist.getArtist());
            System.out.println("Publication date: " + tolist.getDate());
            System.out.println("List of copies: ");
            ArrayList<Copy> listOfCopies = tolist.getCopyList();
            Iterator<Copy> itcopy = listOfCopies.iterator();
            while(itcopy.hasNext())
            {
                Copy tocopy = itcopy.next();
                System.out.println("Serial Number: " + tocopy.getSerialNumber());
                System.out.println("Purchase Date: " + tocopy.getPurchaseDate());
                System.out.println("Purchase Cost: " + tocopy.getPurchaseCost());
                System.out.println();
            }
            System.out.println();
        }
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please, press ENTER to continue: ");
        keyboard.nextLine();
    }
}